import tkinter as tk
from tkinter import ttk, messagebox
import random
import time

class DiceGame:
    def __init__(self, master):
        self.master = master
        self.master.title("骰子游戏")
        self.master.geometry("400x400")
        self.master.resizable(False, False)
        
        # 确保中文显示正常
        self.master.option_add("*Font", "SimHei 10")
        
        # 游戏设置
        self.player_score = 0
        self.computer_score = 0
        self.current_player = "player"  # "player" 或 "computer"
        self.game_active = False
        
        # 创建UI
        self.create_ui()
    
    def create_ui(self):
        """创建用户界面"""
        # 创建顶部标题
        title_label = tk.Label(
            self.master,
            text="骰子游戏",
            font=("SimHei", 16, "bold")
        )
        title_label.pack(pady=10)
        
        # 创建分数显示
        self.score_frame = tk.Frame(self.master)
        self.score_frame.pack(pady=10)
        
        self.player_score_var = tk.StringVar(value="你的分数: 0")
        self.player_score_label = tk.Label(
            self.score_frame,
            textvariable=self.player_score_var,
            font=("SimHei", 12)
        )
        self.player_score_label.pack(side=tk.LEFT, padx=20)
        
        self.computer_score_var = tk.StringVar(value="电脑分数: 0")
        self.computer_score_label = tk.Label(
            self.score_frame,
            textvariable=self.computer_score_var,
            font=("SimHei", 12)
        )
        self.computer_score_label.pack(side=tk.LEFT, padx=20)
        
        # 创建骰子显示
        self.dice_frame = tk.Frame(self.master)
        self.dice_frame.pack(pady=20)
        
        self.dice_label = tk.Label(
            self.dice_frame,
            text="准备开始",
            font=("SimHei", 24, "bold"),
            width=10,
            height=3,
            relief=tk.RAISED
        )
        self.dice_label.pack()
        
        # 创建操作按钮
        self.control_frame = tk.Frame(self.master)
        self.control_frame.pack(pady=20)
        
        self.roll_btn = ttk.Button(
            self.control_frame,
            text="掷骰子",
            command=self.roll_dice,
            width=15
        )
        self.roll_btn.pack(side=tk.LEFT, padx=10)
        
        self.reset_btn = ttk.Button(
            self.control_frame,
            text="重置游戏",
            command=self.reset_game,
            width=15
        )
        self.reset_btn.pack(side=tk.LEFT, padx=10)
        
        # 创建状态显示
        self.status_var = tk.StringVar(value="轮到你掷骰子")
        self.status_label = tk.Label(
            self.master,
            textvariable=self.status_var,
            font=("SimHei", 12)
        )
        self.status_label.pack(pady=10)
    
    def roll_dice(self):
        """掷骰子"""
        if not self.game_active:
            self.game_active = True
            self.current_player = "player"
            self.status_var.set("轮到你掷骰子")
        
        if self.current_player == "player":
            # 玩家掷骰子
            dice_value = random.randint(1, 6)
            self.player_score += dice_value
            
            # 更新UI
            self.dice_label.config(text=str(dice_value))
            self.player_score_var.set(f"你的分数: {self.player_score}")
            
            # 检查是否获胜
            if self.player_score >= 100:
                self.game_active = False
                self.status_var.set("恭喜你赢了！")
                messagebox.showinfo("游戏结束", "恭喜你赢了！")
                return
            
            # 切换到电脑
            self.current_player = "computer"
            self.status_var.set("电脑正在掷骰子...")
            self.master.after(1000, self.computer_turn)
        else:
            # 提示当前是电脑回合
            self.status_var.set("请等待电脑掷骰子...")
    
    def computer_turn(self):
        """电脑回合"""
        # 电脑策略：掷骰子直到达到20分或更高，或者超过玩家分数
        computer_turn_score = 0
        while computer_turn_score < 20 or computer_turn_score < self.player_score - self.computer_score:
            dice_value = random.randint(1, 6)
            computer_turn_score += dice_value
            
            # 更新UI显示电脑当前掷骰子结果
            self.dice_label.config(text=str(dice_value))
            self.master.update()
            time.sleep(0.5)  # 暂停一下，让用户看到电脑的掷骰子过程
        
        self.computer_score += computer_turn_score
        
        # 更新UI
        self.computer_score_var.set(f"电脑分数: {self.computer_score}")
        
        # 检查是否获胜
        if self.computer_score >= 100:
            self.game_active = False
            self.status_var.set("电脑赢了！")
            messagebox.showinfo("游戏结束", "电脑赢了！")
            return
        
        # 切换回玩家
        self.current_player = "player"
        self.status_var.set("轮到你掷骰子")
    
    def reset_game(self):
        """重置游戏"""
        self.player_score = 0
        self.computer_score = 0
        self.current_player = "player"
        self.game_active = False
        
        # 更新UI
        self.player_score_var.set(f"你的分数: {self.player_score}")
        self.computer_score_var.set(f"电脑分数: {self.computer_score}")
        self.dice_label.config(text="准备开始")
        self.status_var.set("轮到你掷骰子")

if __name__ == "__main__":
    root = tk.Tk()
    game = DiceGame(root)
    root.mainloop()